using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShivamEMR.Model.Domain.PersonDomain;
//using ShivamEMR.Web.Areas.Person.Models;
using ShivamEMR.Model.DataInterfaces.PersonDataInterf;
using ShivamEMR.Model;
using ShivamEMR.Web.Models;

namespace ShivamEMR.Web.Areas.Person.Controllers
{   
    public class PersonController : Controller
    {
		private readonly ISexRepository sexRepository;
		private readonly IPersonPictureRepository personpictureRepository;
		private readonly IPersonAccountRepository personaccountRepository;
		private readonly IPersonStatusRepository personstatusRepository;
		private readonly IPersonRepository personRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public PersonController() 
        {
            sexRepository = Registry.RepositoryFactory.GetSexRepository();
            personpictureRepository = Registry.RepositoryFactory.GetPersonPictureRepository();
            personaccountRepository = Registry.RepositoryFactory.GetPersonAccountRepository();
            personstatusRepository = Registry.RepositoryFactory.GetPersonStatusRepository();
            personRepository = Registry.RepositoryFactory.GetPersonRepository();
        }

        public PersonController(ISexRepository sexRepository, IPersonPictureRepository personpictureRepository, IPersonAccountRepository personaccountRepository, IPersonStatusRepository personstatusRepository, IPersonRepository personRepository)
        {
			this.sexRepository = sexRepository;
			this.personpictureRepository = personpictureRepository;
			this.personaccountRepository = personaccountRepository;
			this.personstatusRepository = personstatusRepository;
			this.personRepository = personRepository;
        }

        //
        // GET: /Person/
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Index()
        {
           // return View(personRepository.AllIncluding(person => person.Sex, person => person.Picture, person => person.PersonStatus, person => person.Addresses, person => person.Contacts, person => person.CitizenShips, person => person.Informations, person => person.PersonRoles, person => person.Groups, person => person.Documents, person => person.Assignments, person => person.InteractionsWith));
            return View(personRepository.All);
        }

        //
        // GET: /Person/Details/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ViewResult Details(System.Guid id)
        {
            return View(personRepository.Find(id));
        }

        //
        // GET: /Person/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Create()
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
            var person = new ShivamEMR.Model.Domain.PersonDomain.Person() { DateOfBirth=DateTime.Now };
            return View(person);
        } 

        //
        // POST: /Person/Create
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Create(ShivamEMR.Model.Domain.PersonDomain.Person person)
        {
            if (ModelState.IsValid) {
                personRepository.Create(person);
                personRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Person/Edit/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Edit(System.Guid id)
        {
			ViewBag.PossibleSexes = sexRepository.All;
			ViewBag.PossiblePictures = personpictureRepository.All;
			ViewBag.PossiblePersonAccounts = personaccountRepository.All;
			ViewBag.PossiblePersonStatus = personstatusRepository.All;
             return View(personRepository.Find(id));
        }

        //
        // POST: /Person/Edit/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost]
        public ActionResult Edit(ShivamEMR.Model.Domain.PersonDomain.Person person)
        {
            if (ModelState.IsValid) {
                personRepository.Update(person);
                personRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleSexes = sexRepository.All;
				ViewBag.PossiblePictures = personpictureRepository.All;
				ViewBag.PossiblePersonAccounts = personaccountRepository.All;
				ViewBag.PossiblePersonStatus = personstatusRepository.All;
				return View();
			}
        }

        //
        // GET: /Person/Delete/5
 [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        public ActionResult Delete(System.Guid id)
        {
            return View(personRepository.Find(id));
        }

        //
        // POST: /Person/Delete/5
        [CustomAuthorizeAttribute(RequiredRight = "Admin")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(System.Guid id)
        {
           // personRepository.Delete(id);
            var person = personRepository.Find(id);
              personRepository.Delete(person);
            personRepository.Save();

            return RedirectToAction("Index");
        }
    }
}

